from django.http import Http404
from rest_framework.decorators import api_view
from rest_framework.request import Request
from rest_framework.response import Response

from api.serializers.category_serializer import CategorySerializer
from api.utils.request_util import RequestUtil, REQUEST_UTIL
from app.models import Category


@api_view(['GET'])
def get_category_detail(request: Request, category_id):
    try:
        category = Category.objects.get(id=category_id)
    except Category.DoesNotExist:
        raise Http404
    serializer = CategorySerializer(category)
    return Response(serializer.data)


@api_view(['GET'])
def get_category_list(request: Request):
    data = get_all_category(REQUEST_UTIL)
    return Response(data)


# def get_all_categories():
#     categories = []
#     top_categories = Category.objects.filter(parent_category=None)
#     for category in top_categories:
#         item = make_category_item(category)
#         add_sub_categories(item, category.sub_categories.all())
#         categories.append(item)
#     return categories

#
# def make_category_item(category: Category):
#     return dict(category_id=category.id,
#                 name=category.name,
#                 image=category.image_url,
#                 description=category.description,
#                 sub_categories=[])
#
#
# def add_sub_categories(parent_category_item, categories):
#     if categories is None or len(categories) == 0:
#         return
#
#     for category in categories:
#         item = make_category_item(category)
#         sub_categories = category.sub_categories.all()
#         add_sub_categories(item, sub_categories)
#         parent_category_item['sub_categories'].append(item)

def get_all_category(request_util: RequestUtil):
    '''
    get all categories with Web API
    '''
    data = request_util.query(form_id="SAL_MATERIALGROUP", fields_keys="FID,FName,FParentId")
    category_list = []
    temp = []
    parent_categories = []
    for item in data:
        if item[2] == 0:
            category = create_category(item)
            parent_categories.append(category)
            category_list.append(category)
            temp.append(item)
    for item in temp:
        data.remove(item)
    while len(parent_categories) > 0:
        temp_parent_categories = []
        temp = []
        for parent_category in parent_categories:
            parent_id = parent_category['category_id']
            for item in data:
                if item[2] == parent_id:
                    category = create_category(item)
                    temp_parent_categories.append(category)
                    temp.append(item)
                    parent_category["sub_categories"].append(category)
        for item in temp:
            data.remove(item)
        parent_categories = temp_parent_categories
    return category_list


def create_category(item):
    '''
    translate item to category
    '''
    return {
        "category_id": item[0],
        "name": item[1],
        "parent_category_id": item[2],
        "image": "",
        "description": "",
        "sub_categories": []
    }